<?php

/**
 * MyProjectNameHere <http://www.example.com>
 * Model Class
 *
 * It is recommended to extend Model classes from WWW_Factory in order to 
 * provide various useful functions and API access for the Model.
 *
 * @package    Factory
 * @author     DeveloperNameHere <email@example.com>
 * @copyright  Copyright (c) 2012, ProjectOwnerNameHere
 * @license    Unrestricted
 * @tutorial   /doc/pages/guide_mvc.htm
 * @since      1.0.0
 * @version    1.0.0
 */

class WWW_model_payment extends WWW_Factory {

	/**
	 * It is recommended to define all data variables here. Usually the 
	 * data variables have the same names as the column names of database 
	 * rows from a table.
	 */	
	public $conn;
	/**
	 * Alternative to __construct()
	 *
	 * WWW_Factory does not allow to overwrite __construct() method, so 
	 * this __initialize() is used instead and loaded automatically when 
	 * object is created.
	 *
	 * @return boolean
	 */
	public function __initialize(){
		require_once('engine/class.www-database.php');
		$this->conn=new WWW_Database();				
		
		require_once('engine/include_dao.php');
		return true;
		
	}
	public function create_payment($data)
	{
		$payment = new Payment();
		$payment->admin_id = $data['admin_id'];
		$payment->payment_gateway = $data['payment_gateway'];
		$payment->payment_url = $data['payment_url'];
		$payment->payment_otherinfo = $data['payment_otherinfo'];
		$payment->created_date = date("Y-m-d H:i:s");
		$payment->updated_date = date("Y-m-d H:i:s");
		$result = DAOFactory::getPaymentDAO()->insert($payment);
		//print_r($result);exit;
	
		/* $query="insert into `payment` (payment_gateway,admin_id,payment_url,payment_otherinfo,created_date,updated_date) VALUES ('".$data['payment_gateway']."','".$data['admin_id']."','".$data['payment_url']."','".$data['payment_otherinfo']."','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."')";
		$result=$this->conn->dbCommand($query);
 */		
		if($result !== ''){
				return array('message'=>'payment successfully created');			
			}else{
				return array('message'=>'payment Creation Failed');
		}	
	}
	public function edit_payment($data){
		$payment = new Payment();
		$payment->admin_id = $data['admin_id'];
		$payment->payment_gateway = $data['payment_gateway'];
		$payment->payment_url = $data['payment_url'];
		$payment->payment_otherinfo = $data['payment_otherinfo'];
		$payment->created_date = date("Y-m-d H:i:s");
		$payment->updated_date = date("Y-m-d H:i:s");
		$payment->payment_id = $data['payment_id'];
		$payment->status = $data['status'];
		$result = DAOFactory::getPaymentDAO()->update($payment);
		
		//print_r($result);exit;
	/* $query="update `payment` set payment_gateway='".$data['payment_gateway']."',payment_url='".$data['payment_url']."',payment_otherinfo='".$data['payment_otherinfo']."',admin_id='".$data['admin_id']."',status='".$data['status']."',updated_date='".date("Y-m-d H:i:s")."' where payment_id=".$data['payment_id'];
		$result=$this->conn->dbCommand($query); */
		
		if($result !== ''){
			$paymentUpdate = new PaymentUpdate();
		$paymentUpdate->payment_id = $data['payment_id'];
		$paymentUpdate->admin_id = $data['admin_id'];
		$paymentUpdate->created_date = date("Y-m-d H:i:s");
		$paymentUpdate->updated_date = date("Y-m-d H:i:s");
		$result = DAOFactory::getPaymentUpdatesDAO()->insert($paymentUpdate);
		//print_r($result);exit;
	
	/* $query = "insert into payment_updates (payment_id,admin_id,created_date,updated_date) values('".$data['payment_id']."','".$data['admin_id']."','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."')";
			$result1 = $this->conn->dbCommand($query); */
			if($result !== 0){
			return array('message'=>'Updated Successfully');			
		}else{
			return array('message'=>'Updates Failed');
		}
		}
	}
	public function get_payment($data)
	{
		$payment = new Payment();
		$payment->payment_id = $data['payment_id'];
		$result = DAOFactory::getPaymentDAO()->load($payment);
		//print_r($result);exit;
	
/* 	$query="select p.*,au.first_name,au.last_name from `payment` p left join admin_users au on p.admin_id=au.admin_id  where payment_id=".$data['payment_id'];
	$result=$this->conn->dbMultiple($query);		 */
		if($result !== ''){
			return array('message'=>'Role get Successfully', 'result'=>$result);			
		}else{
			return array('message'=>'Role get Failed');
		}	
	
	
	}
	public function delete_payment($data){
		$payment = new Payment();
		$payment->payment_id = $data['payment_id'];
		$payment->status = $data['status'];
		$result = DAOFactory::getPaymentDAO()->delete($payment);
		//print_r($result);exit;
	
	/* $query = "UPDATE payment SET status='".$data['status']."' WHERE payment_id=".$data['payment_id'];
		$data=$this->conn->dbCommand($query); */
		if($result == 1){
			return array('message'=>"Role Deleted Successfully");		
		}else
			return array('message'=>'Role Deletion Fails');
	}
	
	public function all_payments($data){
	$current_page=0;
	$results_per_page=10;
	$order_by="created_date";
	$sort_order="desc";
	if(isset($data['order_by']) && $data['order_by']!='')
	{
		$order_by=$data['order_by'];
	}
	if(isset($data['sort_order']) && $data['sort_order']!='')
	{
		$sort_order=$data['sort_order'];
	}
	if(isset($data['current_page'])&& $data['current_page']!='' || $data['current_page']!=0)
	{
	$current_page=$data['current_page'];
	}
	if(isset($data['results_per_page']) && $data['results_per_page']!='' || $data['results_per_page']!=0){
	$results_per_page=$data['results_per_page'];
	}
	$offset=$current_page;
		$payment = new Payment();
		$payment->order_by = $order_by;
		$payment->sort_order = $sort_order;
		$payment->offset = $offset;
		$payment->results_per_page = $results_per_page;
		$result = DAOFactory::getPaymentDAO()->all_payment($payment);
		$totalrows = DAOFactory::getPaymentDAO()->payment_count($payment);
		//print_r($result);exit;
	
	/* $query="select p.*,au.first_name,au.last_name from `payment` p left join admin_users au on p.admin_id=au.admin_id order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
	$result=$this->conn->dbMultiple($query);
	 */

	 if($result !== ''){
		/*   $totalrows = DAOFactory::getPaymentDAO()->payment_count($payment);
		print_r($result);exit;
			$totalrows = $this->conn->dbSingle('SELECT count(*) as total from payment'); */
		return array('message'=>"All ","info"=>$result,'total'=>$totalrows,'current_page'=>$current_page,'results_per_page'=>$results_per_page);
		}
		else
		return array('message'=>'No Payments');
	
	}
	public function payment_updates_list($data)
	{	
		$paymentUpdate = new PaymentUpdate();
		$paymentUpdate->payment_id = $data['payment_id'];
		$result = DAOFactory::getPaymentUpdatesDAO()->updates_list($paymentUpdate);
		//print_r($result);exit;
	
	/* $query="select pu.updated_date,au.first_name,au.last_name from `payment_updates` pu left join admin_users au on pu.admin_id=au.admin_id  where payment_id=".$data['payment_id'];
	$result=$this->conn->dbMultiple($query);		 */

	if($result !== ''){
			return array('message'=>'Role get Successfully', 'result'=>$result);			
		}else{
			return array('message'=>'Role get Failed');
		}	
	}
	
}
	
?>